'use strict';

const { BaseParameter } = require('../core/base_mongo_model');

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;

  const workSchema = new Schema({
    ...BaseParameter,
    startTime: {
      type: Date,
      default: new Date(),
    }, // 开始时间
    endTime: {
      type: Date,
      default: new Date(),
    }, // 结束时间
    name: { type: String, default: '' }, // 公司名称
    icon: { type: String, default: '' }, // 公司图片
    detail: { type: String, default: '' }, // 详情
    obligation: { type: String, default: '' }, // 职责
    url: { type: String, default: '' }, // 官网
  });

  const skillsSchema = new Schema({
    name: { type: String, default: '', max: 100, min: 1 }, // 名称
    show: { type: String, default: '', max: 1000, min: 1 },
    num: { type: Number, default: 0, max: 100, min: 0 },
  });

  const MeSchema = new Schema({
    home_icon: {
      type: String,
      default: '',
    }, // 主页头像
    home_videos: { type: [ String ], default: [] }, // 主页标题
    home_title: { type: String, default: '' }, // 主页标题
    home_jianli: { type: String, default: '' }, // 简历链接
    abouts: { type: String, default: '' }, // 关于我
    expertise: { type: String, default: '' }, // 专业技能
    skills: [ skillsSchema ],
    experience_show: { type: String, default: '' }, // 工作经验说明
    works: [ workSchema ], // 公司
    works_show: { type: String, default: '' }, // 工作经历
  });

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 更新数据
   */
  MeSchema.statics.updateModel = function(id, upData) {
    return this.updateOne({
      _id: mongoose.Types.ObjectId(id),
    }, upData);
  };

  MeSchema.statics.addSkills = function(data) {
    return this.updateOne({}, {
      $push: {
        skills: data
      }
    })
  };

  MeSchema.statics.removeSkills = function(id){
    return this.updateOne({}, {
      $pull: {
        skills: {
          _id: mongoose.Types.ObjectId(id),
        }
      }
    })
  }

  MeSchema.statics.updateSkills = function(id, data){
    return this.updateOne(
      {
        'skills._id': mongoose.Types.ObjectId(id),
      },
      {
        $set: {
          'skills.$': data
        }
      })
  }

  MeSchema.statics.addWork = function(data) {
    return this.updateOne({}, {
      $push: {
        works: data
      }
    })
  };

  MeSchema.statics.removeWork = function(id){
    return this.updateOne({}, {
      $pull: {
        works: {
          _id: mongoose.Types.ObjectId(id),
        }
      }
    })
  }

  MeSchema.statics.updateWork = function(id, data){
    return this.updateOne(
      {
        'works._id': mongoose.Types.ObjectId(id),
      },
      {
        $set: {
          'works.$': data
        }
      })
  }

  return mongoose.model('Me', MeSchema, 'me');
};
